package com.watson.todolist;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * Custom Item Overlay class to show images as markers in our google map.
 * 
 * @author Donnie Ward
 * 
 * @version 1.0
 *
 */
public class CustomItemizedOverlay extends ItemizedOverlay{
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	
	public CustomItemizedOverlay(Drawable defaultMarker) {
		  super(boundCenterBottom(defaultMarker));
	}
	
	/**
	 * This method adds an overlay to the List of overlays for the map to draw.
	 * 
	 * @param overlay - The overlay to add
	 */
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}

	/**
	 * This method returns an item from the List of overlays with the specified index
	 * 
	 * @param index
	 * @return overlay
	 */
	@Override
	protected OverlayItem createItem(int i) {
	  return mOverlays.get(i);
	}

	/**
	 * This method gets the size of the arrayList of overlays
	 * 
	 * @return size
	 */
	@Override
	public int size() {
	  return mOverlays.size();
	}
	
	/**
	 * Creates an itemized overlay and gives it a context
	 * 
	 * @param default marker
	 * @paran context
	 */
	public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
	}
	
	/**
	 * Sets the message displayed when a user taps on a marker
	 * 
	 * @param index
	 */
	@Override
	protected boolean onTap(int index) {
	  OverlayItem item = mOverlays.get(index);
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(item.getTitle());
	  dialog.setMessage(item.getSnippet());
	  dialog.show();
	  return true;
	}
}
